package managedbean.SCM.DeliveryOrder;

import entity.SCM.DeliveryOrder;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import session.stateless.SCM.DeliveryOrderBeanLocal;

@ManagedBean (name = "pendingDeliveryOrderManagerBean")
@ViewScoped
public class PendingDeliveryOrderManagerBean implements Serializable{

    public PendingDeliveryOrderManagerBean() {
    }
    
    @EJB
    private DeliveryOrderBeanLocal deliveryOrderBeanLocal;
    private DeliveryOrder selectedDeliveryOrder;
    private String statusMessage;

    public DeliveryOrder getSelectedDeliveryOrder() {
        return selectedDeliveryOrder;
    }

    public void setSelectedDeliveryOrder(DeliveryOrder selectedDeliveryOrder) {
        this.selectedDeliveryOrder = selectedDeliveryOrder;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }
    
    
    public List<DeliveryOrder> getPendingDeliveryOrder(){
        return deliveryOrderBeanLocal.getPendingDeliveryOrder();
    }
    
    public void processDeliveryOrder(ActionEvent event){
        try{
            
            boolean success = deliveryOrderBeanLocal.processDeliveryOrder(selectedDeliveryOrder.getId());
            
            if(!success){
                System.out.println("test1");
                statusMessage = "Please Try Again.";
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Unsuccessful:" + statusMessage + "", ""));
            }else{
                System.out.println("test2");
                statusMessage = "Process Delivery Order Successfully.";
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessage, ""));
            }
            
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }
}
